package com.isnmp.common.snmp;

import java.io.FileOutputStream;
import java.io.PrintStream;

import net.percederberg.mibble.MibType;
import net.percederberg.mibble.MibValueSymbol;
import net.percederberg.mibble.snmp.SnmpObjectType;

import com.isnmp.common.snmp.impl.SNMPAPIImpl;

public class GenJavaCodeTool {
    private static String srcDirectory = "";
    private static String packageName = "";

    public static void setJavaCodeDirectory(String paramString1, String paramString2) {
        srcDirectory = paramString1;
        packageName = paramString2;
    }

    private static String getType(SnmpObjectType paramSnmpObjectType) {
        MibType localMibType = paramSnmpObjectType.getSyntax();
        if (localMibType.hasTag(0, 2))
            return "int";
        if (localMibType.hasTag(1, 1))
            return "long";
        if (localMibType.hasTag(1, 2))
            return "long";
        if (localMibType.hasTag(0, 4))
            return "String";
        if (localMibType.hasTag(0, 6))
            return "String";
        if (localMibType.hasTag(1, 0))
            return "String";
        if (localMibType.hasTag(1, 3))
            return "long";
        if (localMibType.hasTag(1, 4))
            return "String";
        if (localMibType.hasTag(1, 6))
            return "long";
        System.err.print("未知的SNMP MIB数据类型: " + localMibType + "，  将用String类型来映射（仅用于SNMP查询操作）");
        return "    String";
    }

    public static void genJavaCode(String paramString1, String paramString2) {
        try {
            MibValueSymbol localMibValueSymbol1 = SNMPAPIImpl.getInstance().getMibSymbolByOid(paramString2);
            PrintStream localPrintStream = new PrintStream(new FileOutputStream(srcDirectory + paramString1 + ".java"));
            System.setOut(localPrintStream);
            System.out.println("package " + packageName + " ;");
            System.out.println("//GenJavaCodeTool自动生成的OM对象代码！");
            System.out.println("public class " + paramString1 + " extends  com.isnmp.common.snmp.OMMappingInfo");
            System.out.println("{");
            MibValueSymbol[] arrayOfMibValueSymbol = localMibValueSymbol1.getChildren();
            Object localObject1;
            Object localObject2;
            Object localObject3;
            for (int i = 0; i < arrayOfMibValueSymbol.length; i++) {
                MibValueSymbol localMibValueSymbol2 = arrayOfMibValueSymbol[i];
                if ((!localMibValueSymbol2.isTable()) && (!localMibValueSymbol2.isTableRow())) {
                    localObject1 = localMibValueSymbol2.getName();
                    localObject2 = (SnmpObjectType) localMibValueSymbol2.getType();
                    localObject3 = getType((SnmpObjectType) localObject2);
                    System.out.println("    private " + (String) localObject3 + " " + (String) localObject1 + " ;");
                    System.out.println();
                }
            }
            System.out.println("    public String toString()");
            System.out.println("    {");
            StringBuffer localStringBuffer = new StringBuffer("        return ");
            for (int j = 0; j < arrayOfMibValueSymbol.length; j++) {
                localObject1 = arrayOfMibValueSymbol[j];
                if ((!((MibValueSymbol) localObject1).isTable()) && (!((MibValueSymbol) localObject1).isTableRow())) {
                    localObject2 = ((MibValueSymbol) localObject1).getName();
                    localStringBuffer.append("\"" + (String) localObject2 + "=\"+" + (String) localObject2 + "+\"|\"+");
                }
            }
            localStringBuffer.deleteCharAt(localStringBuffer.length() - 1);
            localStringBuffer.append(" ;");
            System.out.println(localStringBuffer);
            System.out.println("    }");
            System.out.println();
            System.out.println("    public String getMappingOID()");
            System.out.println("    {");
            System.out.println("        return \"" + paramString2 + "\";");
            System.out.println("    }");
            for (int j = 0; j < arrayOfMibValueSymbol.length; j++) {
                localObject1 = arrayOfMibValueSymbol[j];
                if ((!((MibValueSymbol) localObject1).isTable()) && (!((MibValueSymbol) localObject1).isTableRow())) {
                    localObject2 = ((MibValueSymbol) localObject1).getName();
                    localObject3 = (SnmpObjectType) ((MibValueSymbol) localObject1).getType();
                    String str1 = getType((SnmpObjectType) localObject3);
                    String str2 = ((String) localObject2).replaceFirst(((String) localObject2).substring(0, 1), ((String) localObject2).substring(0, 1).toUpperCase());
                    System.out.println("    public " + str1 + " get" + str2 + "()");
                    System.out.println("    {");
                    System.out.println("        return " + (String) localObject2 + " ;");
                    System.out.println("    }");
                    System.out.println("    public void set" + str2 + "(" + str1 + " value)");
                    System.out.println("    {");
                    System.out.println("        " + (String) localObject2 + "=value ;");
                    System.out.println("    }");
                }
            }
            System.out.println("}");
            localPrintStream.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        System.setOut(null);
    }

    public static void main(String[] paramArrayOfString) {
    }
}